home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / mpsql-1.001 / mpsql-1~ / mpsql-1.0 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-28  |  5.1 KB  |  156 lines

  1. /************************************************************************/
  2. /* Module : util.c                                    */
  3. /* Purpose: utility module for Mpsql                                  */
  4. /* By     : Keith R. Davis                            */
  5. /* Date   : 12/8/95                                    */
  6. /* Notes  : Copyright(c) 1996 White River Software                      */
  7. /*          Copyright(c) 1994, Regents of the University of California  */
  8. /************************************************************************/
  9.  
  10. #include <string.h>        /* string lib header                */
  11. #include <malloc.h>             /* malloc header                        */
  12.  
  13. #include "util.h"               /* util func. header                    */
  14.  
  15. /* all routines assume null-terminated strings!                         */
  16.  
  17. /* removes whitespaces from the left, right and both sides of a string  */
  18. /* MODIFIES the string passed in and returns the head of it             */
  19.  
  20. /************************************************************************/
  21. /* Function: leftTrim                                                   */
  22. /* Purpose : trims whitespace from left side of a string                */
  23. /* Params  : s : char string to trim                                    */
  24. /* Returns : ptr to resulting string                                    */
  25. /* Notes   :                                                            */
  26. /************************************************************************/
  27.  
  28. char* leftTrim(char* s)  
  29. {
  30.   char* s2 = s;
  31.   int shift=0;
  32.   int j=0;
  33.  
  34.   while (isspace(*s))
  35.     { s++; shift++;}
  36.   if (shift > 0)
  37.     {
  38.       while ( (s2[j] = s2[j+shift]) !='\0')
  39.     j++;
  40.     }
  41.  
  42.   return s2;
  43. }
  44.  
  45. /************************************************************************/
  46. /* Function: rightTrim                                                  */
  47. /* Purpose : trims whitespace from right side of a string               */
  48. /* Params  : s : char string to trim                                    */
  49. /* Returns : ptr to resulting string                                    */
  50. /* Notes   :                                                            */
  51. /************************************************************************/
  52.  
  53. char* rightTrim(char* s)
  54. {
  55.   char* sEnd;
  56.   sEnd = s+strlen(s)-1;
  57.   while (isspace(*sEnd))
  58.     sEnd--;
  59.   if (sEnd < s)
  60.     s[0]='\0';
  61.   else
  62.     s[sEnd-s+1]='\0';
  63.   return s;
  64. }
  65.  
  66. /************************************************************************/
  67. /* Function: doubleTrim                                                 */
  68. /* Purpose : trims whitespace from both sides of a string               */
  69. /* Params  : s : char string to trim                                    */
  70. /* Returns : ptr to resulting string                                    */
  71. /* Notes   :                                                            */
  72. /************************************************************************/
  73.  
  74. char* doubleTrim(char* s)
  75. {
  76.   strcpy(s,leftTrim(rightTrim(s)));
  77.   return s;
  78. }
  79.  
  80. /************************************************************************/
  81. /* Function: dupstr                                                     */
  82. /* Purpose : copies a string, while allocating space for it.            */
  83. /*           the CALLER is responsible for freeing the space            */
  84. /* Params  : s : char string to trim                                    */
  85. /* Returns : ptr to resulting string                                    */
  86. /* Notes   : returns NULL if the argument is NULL                       */
  87. /************************************************************************/
  88.  
  89. char* dupstr(char *s)
  90. {
  91.   char* result;
  92.  
  93.   if (s == NULL)
  94.     return NULL;
  95.  
  96.   result = (char*)malloc(strlen(s)+1);
  97.   strcpy(result, s);
  98.   return result;
  99. }
  100.  
  101. /************************************************************************/
  102. /* Function: reverse                                    */
  103. /* Purpose : reverses the order of a character string                */
  104. /* Params  : s : ptr to string to be reversed                    */
  105. /* Returns : nothing                                                    */
  106. /* Notes   :                                             */
  107. /************************************************************************/
  108.  
  109. void reverse(char *s)
  110. {
  111.     int    c;        /* character value        */
  112.     int    i;        /* loop counter        */
  113.     int    j;        /* loop counter        */
  114.  
  115.     for(i=0, j = strlen(s) - 1; i < j; i++, j--)
  116.     {
  117.     c = s[i];
  118.     s[i] = s[j];
  119.     s[j] = c;
  120.     }     
  121.     
  122. }
  123.  
  124. /************************************************************************/
  125. /* Function: itoa                                    */
  126. /* Purpose : converts a int to a char string                    */
  127. /* Params  : n : integer to convert to string                    */        
  128. /*       : s : ptr to array to hold the converted int value            */
  129. /* Returns : ptr to the char array (s)                            */
  130. /* Notes                                                                */
  131. /************************************************************************/
  132.  
  133. char* itoa(int n, char *s)
  134. {
  135.     int    i;        /* loop counter        */
  136.     int    sign;        /* int sign            */
  137.  
  138.     if((sign = n) < 0)
  139.     n = -n;
  140.     i = 0;
  141.  
  142.     do
  143.     {
  144.     s[i++] = n % 10 + '0';
  145.     } while ((n /= 10) > 0);
  146.  
  147.     if(sign < 0)
  148.     s[i++] = '-';
  149.     s[i] = '\0';
  150.  
  151.     reverse(s);
  152.  
  153.     return(s);
  154. }
  155.  
  156.